package com.qiyu.mall.user.provider.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageRowBounds;
import com.qiyu.core.cache.redis.app.id.IdService;
import com.qiyu.mall.user.provider.dao.AccountDao;
import com.qiyu.mall.user.provider.entity.AccountPO;
import com.qiyu.mall.user.interfaces.dto.AccountDTO;
import com.qiyu.mall.user.interfaces.dto.AccountReqDTO;
import com.qiyu.mall.user.interfaces.dto.LoginDTO;
import com.qiyu.mall.user.interfaces.dto.WxPhoneDetailDTO;
import com.qiyu.mall.user.interfaces.service.AccountService;
import com.qiyu.mall.user.interfaces.service.AuthService;
import com.qiyu.mall.user.provider.utils.BeanCopyUtil;
import com.qiyu.mall.user.provider.utils.LocalCacheUtil;
import com.qiyu.mall.user.provider.utils.WechatDecodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author linhao
 * @date 2019/12/14
 * @Version V1.0
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountDao accountDao;
    @Resource
    private IdService idService;
    @Resource
    private AuthService authService;

    @Override
    public AccountDTO getAccountDTO(Integer accountId) {
        if (LocalCacheUtil.ACCOUNT_CACHE.containsKey(accountId)) {
            return LocalCacheUtil.ACCOUNT_CACHE.get(accountId);
        }
        AccountPO accountPO = accountDao.selectOneByAccountId(accountId);
        if (accountPO != null) {
            AccountDTO accountDTO = new AccountDTO();
            BeanUtils.copyProperties(accountPO, accountDTO);
            LocalCacheUtil.ACCOUNT_CACHE.put(accountId, accountDTO);
            return accountDTO;
        }
        return null;
    }

    @Override
    public WxPhoneDetailDTO saveAccountPhone(String encryptedData, String token, String iv) {
        LoginDTO loginDTO = authService.checkToken(token);
        JSONObject jsonObject = (JSONObject) WechatDecodeUtils.getPhoneNumber(encryptedData, loginDTO.getSessionKey(), iv);
        WxPhoneDetailDTO wxPhoneDetailDTO = new WxPhoneDetailDTO();
        String jsonStr = jsonObject.toJSONString();
        wxPhoneDetailDTO = JSONObject.parseObject(jsonStr, WxPhoneDetailDTO.class);
        log.info("解析数据信息为{}", wxPhoneDetailDTO);
        accountDao.updatePhoneNumber(loginDTO.getAccountId(), wxPhoneDetailDTO.getPhoneNumber());
        return wxPhoneDetailDTO;
    }

    @Override
    public AccountDTO updateWxInfo(AccountDTO accountDTO) {
        accountDao.updateWxInfo(accountDTO.getWxAvatarUrl(), accountDTO.getWxNickName(), accountDTO.getAccountId());
        return accountDTO;
    }

    @Override
    public AccountDTO getByOpenId(String openId) {
        AccountPO accountPO = accountDao.selectOneByOpenId(openId);
        if (accountPO != null) {
            AccountDTO accountDTO = new AccountDTO();
            BeanUtils.copyProperties(accountPO, accountDTO);
            return accountDTO;
        }
        return null;
    }

    @Override
    public void updateLoginTime(String openId) {
        accountDao.updateLoginTime(openId);
    }

    @Override
    public AccountDTO saveAccount(String nickName, String openId) {
        AccountDTO accountDTO = AccountDTO.builder()
                .accountId(idService.generateId())
                .nickname(nickName)
                .openId(openId)
                .lastLoginTime(new Date())
                .accountType(1).build();
        AccountPO accountPO = new AccountPO();
        BeanUtils.copyProperties(accountDTO, accountPO);
        accountDao.insert(accountPO);
        return accountDTO;
    }

    @Override
    public List<AccountDTO> selectAccountDTOList(AccountReqDTO accountReqDTO) {
        int offset = ((accountReqDTO.getPage() - 1) * accountReqDTO.getPageSize());
        EntityWrapper<AccountPO> ew = new EntityWrapper();
        if (accountReqDTO.getAccountId() != null && accountReqDTO.getAccountId() != 0) {
            ew.eq("account_id", accountReqDTO.getAccountId());
        }
        if (accountReqDTO.getNickname() != null && accountReqDTO.getNickname() != "") {
            ew.like("nickname", accountReqDTO.getNickname());
        }
        if (accountReqDTO.getAccountType() != null && accountReqDTO.getAccountType() != "") {
            ew.eq("account_type", accountReqDTO.getAccountType());
        }
        if (accountReqDTO.getTrueName() != null && accountReqDTO.getTrueName() != "") {
            ew.like("true_name", accountReqDTO.getTrueName());
        }
        List<AccountPO> accountPOList = accountDao.selectPage(new PageRowBounds(offset, accountReqDTO.getPageSize()), ew);
//        List<AccountPO> accountPOList = accountDao.selectPage(new PageRowBounds(1, 1), ew);
        List<AccountDTO> accountDTOList = new ArrayList<>(accountPOList.size());
        accountDTOList = BeanCopyUtil.copyList(accountPOList, AccountDTO.class);
        return accountDTOList;
    }

}
